package com.example.yiparkapplication.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class CompressImage {
    /**
     * 图片按比例大小压缩方法
     * @param srcPath （根据路径获取图片并压缩）
     * @return
     */
    public static Bitmap getImage(String srcPath){
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
//        如果inJustDecoedBounds设置为true的话，解码bitmap时可以只返回其高、宽和Mime类型，而不必为其申请内存，从而节省了内存空间
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float hh = 800f;
        float ww = 480f;
        int be = 1;
        if (w > h && w > ww){
            //如果图片宽比高长，而且原图宽度超过480f，就用图片宽度/480 ，得到一个比例，把宽度强制缩小到480f,高度可可以根据这个比例缩小到800f
            be = (int)(newOpts.outWidth / ww);
        }else if (w < h && h > hh){
            //长和宽，反正谁长一些，就按照长的来缩放
            be = (int)(newOpts.outHeight / hh);
        }
        if (be <= 0){
            be = 1;
        }
        //专门用来设置图片缩放比例的参数
        newOpts.inSampleSize = be;
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        //质量压缩
        return compressImage(bitmap);
    }

    /**
     *
     * @param image
     * @return
     */
    public static Bitmap compressImage(Bitmap image){
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        int options = 90;

        //比特长度除以1024获取图片的kb大小，如果图片质量大于100kb,则继续压缩
        while(byteArrayOutputStream.toByteArray().length / 1024 > 100){
            byteArrayOutputStream.reset();
            //options表示压缩质量，数字越低，压缩后的图片越小，质量越差
            //而且压缩图片调用的是Bitmap.compress本身的方法来的
            image.compress(Bitmap.CompressFormat.JPEG, options, byteArrayOutputStream);
            options -= 10;
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream, null, null);
        return bitmap;
    }

    /**
     *
     * @param bitmap
     * @param index
     * @return
     */
    public static File getFile(Bitmap bitmap, int index){
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        File file = new File("/storage/emulated/0/DCIM/Camera/temp" + index + ".jpg");
        try{
            file.createNewFile();
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            int x = 0;
            byte[] b = new byte[1024 * 100];
            while((x = inputStream.read(b)) != -1){
                fileOutputStream.write(b, 0, x);
            }
            fileOutputStream.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return file;
    }

}
